/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package etf.pia.controlers;

import etf.pia.entity.*;
import javax.faces.bean.SessionScoped;
import lombok.Getter;
import lombok.Setter;
import etf.pia.entity.Predmet;
import etf.pia.util.Initializer;
import etf.pia.util.ServerUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Set;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Alejandro
 */
@Getter
@Setter
@ManagedBean(name = "departmentBean")
@SessionScoped
public class DepartmentBean implements Serializable {

    @ManagedProperty("#{loginMediator.currUser.student}")
    private Student student;

    private ArrayList<ArrayList<Predmet>> predmetList = null;
    private String labela;

    public String getPredmetById(int id) {
        //TODO
        return null;
    }

    public String getPredmetsByDepartment(String d) {
        labela = d;
        Session ses = ServerUtil.getSessionFactory().getCurrentSession();
        Transaction t = ses.beginTransaction();
        try {
            Criteria c = ses.createCriteria(Katedra.class);
            Katedra k = (Katedra) c.add(Restrictions.eq("opis", d)).uniqueResult();
            createEmptyList();
            if (k == null) {
                t.commit();
            } else {
                makeListOfLists(extractDepartment(d));
                t.commit();
            }
        } catch (Exception e) {
            t.rollback();
            e.printStackTrace();
        }
        return "departmentPage?faces-redirect=true";
    }

    private void createEmptyList() {
        predmetList = new ArrayList<ArrayList<Predmet>>();
        for (int i = 0; i < 8; i++) {
            predmetList.add(new ArrayList<Predmet>());
        }
    }

    private ArrayList<Predmet> extractDepartment(String d) {
        ArrayList<Predmet> temp = new ArrayList<Predmet>();
        for(Predmet p :  new ArrayList<Predmet>(Initializer.toList(student))){
            if(d.equals(p.getKatedra().getOpis())){
                temp.add(p);
            }
        }
        return temp;
    }

    private void makeListOfLists(ArrayList<Predmet> temp) {
        for (Predmet p : temp) {
            switch (p.getSemestar() - 1) {
                case 0:
                    predmetList.get(0).add(p);
                    break;
                case 1:
                    predmetList.get(1).add(p);
                    break;
                case 2:
                    predmetList.get(2).add(p);
                    break;
                case 3:
                    predmetList.get(3).add(p);
                    break;
                case 4:
                    predmetList.get(4).add(p);
                    break;
                case 5:
                    predmetList.get(5).add(p);
                    break;
                case 6:
                    predmetList.get(6).add(p);
                    break;
                case 7:
                    predmetList.get(7).add(p);
                    break;
            }
        }
    }
}
